class ReadController < ApplicationController

  def index
    redirect_to :action => "latest_issue"
  end

  def latest_issue

    @issue = Issue.last_issue_published
    if !@issue.nil?
      redirect_to :action => "read_issue", :year => @issue.date_published.year, :month => @issue.date_published.month, :day => @issue.date_published.day
    else
        # TODO: Change this for a nice friendly template... 
        render :inline => "No much to see, go away "
    end
  end


  #TODO: Sanitaze data
  def show_section
       date = params[:year].to_s+"-"+params[:month].to_s+"-"+params[:day].to_s
       
       @issue = Issue.find_by_date_published(date)

      if !@issue.nil? and @issue.show?(Time.now())
        @section = Section.find_by_issue_and_name(@issue, params[:name])

     else
        render :inline => "The section you are looking for does not exist."
      end
     
      
  end
  
  def show_article
       date = params[:year].to_s+"-"+params[:month].to_s+"-"+params[:day].to_s
       @issue = Issue.find_by_date_published(date)
       if !@issue.nil? and @issue.show?(Time.now())
          @section = Section.find_by_issue_and_name(@issue, params[:name])
          # We already have the articles from the relation with sections, so dont query again, use 
          # container operations
          # I didn't use sections.articles.find directly, becouse Ruby gets confused between ActiveRecord::find
          # and Array::find
          #articles = @section.articles.map { |ar|  ar.attributes if ar.attributes["id"] == params[:id]}
          # WARNING: Article is not gong to be able as an object in the view...acces it as article["property"]
          @article = @section.articles.find(params[:id])
       else
          render :inline => "The article you are lookin for does not exist"
        end
        
  end
  
  def read_issue

    date = params[:year].to_s+"-"+params[:month].to_s+"-"+params[:day].to_s
    date = date.to_time

    begin
        @issue = Issue.find_by_date_published(date)
    rescue
        render :inline => 'The issue you are looking for does not exist'
    end

    if !@issue.nil? and @issue.show?(Time.now())
      @sections = @issue.sections if ! @issue.nil?
      @articles = []
        
        # Try to get the layout for this issue
      begin
    
        layout = @issue.get_layout
        for element in layout
          @articles << {:article=>Article.find(element["article"]), :section => Section.find(element["section"]).name}
        end
    
        # If no layout is defined (or is not well-defined)
        # get a list of all the articles in the issue 
        rescue Exception => e
    
              if( ! @sections.nil?  and @sections.count > 0 )
                for section in @sections
                  for article in section.articles
                    @articles << {:article=>article, :section => section.name}
                  end
                end
              end
        render :inline=>e.message
        end
      else
        render :inline => "The issue you are looking for does not exist"
      end
      
  end
  
  def show_volume 
  end
  
  def archive
    @volumes = Volume.find ( :all, :order => 'created_at DESC ' )
    @volumes =  ( (@volumes.map {| x | x if x.issues.count() >= 1 } ).compact)
  end
  
# TODO: Pagination, sorting, grouping
  def search
    @articles = Article.find_by_all (params[:query])
# Set only the articles that are accessible TODO: Move this to the model as an option
# in find_by_all 
    @articles =  ( (@articles.map {| x | x if x.readable_from(Time.now()).length >= 1 } ).compact)
    @query = params[:query];
  end

end
